﻿#include "zz.h"

#if defined(__QG_H__)

//#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

using namespace tpl;

void draw_grid(float min, float max, float step)
{
	qgRdh* rdh = qgRdh::Instance;

	rdh->SetTM(QGTM_WORLD, Matrices::Identity);

	float rmax = max + step;

	VecArray<Vec3> ps;

	for (float f = min; f < rmax; f += step)
	{
		ps.Add(Vec3(f, 0.0f, min));
		ps.Add(Vec3(f, 0.0f, max));

		ps.Add(Vec3(min, 0.0f, f));
		ps.Add(Vec3(max, 0.0f, f));
	}

	rdh->DrawLines(false, false, ps.Count(), ps.Data(), NULL);
}

int main(void)
{
	k_setlv(KLV_ALL);
	k_mesgs("test \n");

	qgRdh* rdh = qgRdh::New("", 640, 480, QGSDF_LAYOUT, "test");

	if (rdh)
	{
		qgStub* stub = rdh->GetStub();

		qgFreeType* font = qgFreeType::New();

		qgTexture* tex = rdh->CreateTexture("haha", 64, 64, 0, 0, 0, QGCF_NONE);
		qgTexture* img = rdh->CreateTexture("e:\\z.png", 0);

		qgFpsCamera* cam = qgFpsCamera::New();
		cam->SetProcessLayout(true);
		cam->SetMoveSpeed(50.0f);
		cam->SetLocation(Vec3(0.0f, 100.0f, -450.0f));

		static qbLayoutElement s_le[] =
		{
			{QGDLOU_POSITION, 0, QGDLOT_FLOAT3, 0, 0, },
			{QGDLOU_TEXTURE, 0, QGDLOT_FLOAT2, 0, 0, },
			{QGDLOU_NORMAL, 0, QGDLOT_FLOAT3, 0, 0, },
			{QGDLOU_TANGENT, 0, QGDLOT_FLOAT3, 0, 0, },
			{QGDLOU_BINORMAL, 0, QGDLOT_FLOAT3, 0, 0, },
		};
		qgVertexLayout* lo = qgRdhSupp::CreateVertexLayout(s_le);

		qgShader* shd = rdh->CreateShader(NULL);
		if (!shd->BindFile(QGSHADER_VS, "e:\\zz.hlsl", 0))
			k_breakpoint();
		if (!shd->BindFile(QGSHADER_PS, "e:\\zz.hlsl", 0))
			k_breakpoint();
		shd->Link();

#if 0
		qgSmm* smm = qgSmm::New(0, "e:\\pst1.smm", 0);
#else
		qgSmm* smm = qgSmm::New(0, "e:\\zzxx.smm", 0);
#endif
		for (int i = 0; i < smm->GetMeshCount(); i++)
		{
			qgSmmMeshRef m = smm->GetMeshNth(i);
			m->SetShader(shd, lo);
		}

		float rot = 0.0f;

		while (rdh->Run())
		{
			float adv = stub->GetAdvance();

			if (rdh->Enter())
			{
				//
				cam->Update(adv);
				rdh->SetTM(QGTM_PROJ, cam->GetProjTM());
				rdh->SetTM(QGTM_VIEW, cam->GetViewTM());

				// 모델
				rdh->SetPropVec4(0, Vec4(*(Vec3*)&cam->GetViewTM()._41, 1.0f));	// 카메라
				rdh->SetPropVec4(1, Vec4(0.0f, 500.0f, -500.0f, 1.0f));	// 라이트
				rot = k_cprad(rot + KM_PI_HALF * adv);

				smm->SetRotation(Quat::RotationY(rot));
				smm->SetLocation(Vec3(0.0f, 0.0f, 0.0f));
				smm->Update(adv);
				smm->Draw();

				// 라인
				rdh->SetTM(QGTM_WORLD, tpl::Matrices::Identity);
				static Vec3 vv[] =
				{
					Vec3(0.0f, 0.0f, 0.0f),
					Vec3(0.0f, 500.0f, 0.0f),
				};
				static Vec3 vd[] =
				{
					Vec3(10.0f, 10.0f, 0.0f),
					Vec3(300.0f, 300.0f, 0.0f),
				};
				static ColorValue cc[] =
				{
					ColorValues::Red,
					ColorValues::Blue,
				};
#if 1
				qgRdhSupp::DrawLines(false, false, vv, cc);
#else
				qgRdhSupp::DrawLines(true, false, vv, cc);
#endif

				//
				draw_grid(-1000.0f, 1000.0f, 200.0f);

				rdh->DrawSprite(Rect(50, 50, 200, 200), ColorValues::Cyan);

				//
				font->WriteFormat(5, 5, 0, "TEST \xEC\x9E\x98\xEB\x90\x98\xEB\x9D\xBC! %.2f", stub->GetRunFps());

				//
				rdh->Leave();
				rdh->Flush();
			}
		}

		font->Unload();

		rdh->Unload();
	}

	return 0;
}

#endif
